package com.huawei.appgallery.forum.section.view.widget;

import android.content.Context;
import android.os.Build.VERSION;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.LinearLayout;
import android.widget.OverScroller;
import com.huawei.hms.framework.network.restclient.hwhttp.route.IPRoute;
import java.lang.ref.WeakReference;
import o.bqx;
import o.caz;
import o.ContentManager;
import o.epv;

public class SectionNestScrollLayout extends LinearLayout {
    private static float ʾ = ((float) (Math.log(0.78d) / Math.log(0.9d)));
    private int ʻ;
    private c ʻॱ;
    private int ʼ;
    private boolean ʼॱ;
    private VelocityTracker ʽ;
    private float ʽॱ = ViewConfiguration.getScrollFriction();
    private float ʿ;
    private b ˈ;
    float ˊ;
    private int ˊॱ;
    public View ˋ;
    private float ˋॱ;
    public View ˎ;
    public LinearLayout ˏ;
    private boolean ˏॱ = false;
    private boolean ͺ;
    public LinearLayout ॱ;
    private int ॱˊ;
    private int ॱˋ = 0;
    private boolean ॱˎ;
    private int ॱॱ;
    private boolean ॱᐝ = false;
    private OverScroller ᐝ;
    private boolean ᐝॱ = false;

    public interface c {
        void ˏ(int i, int i2);

        boolean ॱ();
    }

    static class b extends Handler {
        private int ˋ = 0;
        WeakReference<View> ˎ;
        WeakReference<caz> ˏ;

        b(caz o_caz, View view) {
            this.ˏ = new WeakReference(o_caz);
            this.ˎ = new WeakReference(view);
        }

        public void handleMessage(Message message) {
            if (this.ˎ != null && this.ˏ != null) {
                View view = (View) this.ˎ.get();
                if (view != null && message.what == 1001) {
                    if (this.ˋ == view.getScrollY()) {
                        caz o_caz = (caz) this.ˏ.get();
                        if (o_caz != null) {
                            o_caz.ˎ(0);
                            return;
                        }
                        return;
                    }
                    sendMessageDelayed(obtainMessage(1001), 200);
                    this.ˋ = view.getScrollY();
                }
            }
        }
    }

    public void setImmerseView(View view) {
        this.ˋ = view;
    }

    public SectionNestScrollLayout(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        setOrientation(1);
        this.ᐝ = new OverScroller(context);
        this.ʼ = ViewConfiguration.get(context).getScaledTouchSlop();
        this.ʻ = ViewConfiguration.get(context).getScaledMaximumFlingVelocity();
        this.ॱॱ = ViewConfiguration.get(context).getScaledMinimumFlingVelocity();
        this.ʿ = ((context.getResources().getDisplayMetrics().density * 160.0f) * 386.0878f) * 0.84f;
    }

    private void ˏ() {
        if (this.ʽ == null) {
            this.ʽ = VelocityTracker.obtain();
        }
    }

    private void ˊ() {
        if (this.ʽ != null) {
            this.ʽ.recycle();
            this.ʽ = null;
        }
    }

    public boolean dispatchTouchEvent(MotionEvent motionEvent) {
        int action = motionEvent.getAction();
        float y = motionEvent.getY();
        switch (action) {
            case 0:
                this.ˋॱ = y;
                this.ˊ = y;
                break;
            case 2:
                float f = y - this.ˊ;
                if (Math.abs(y - this.ˋॱ) > ((float) this.ʼ)) {
                    boolean z = ˋ() && this.ॱˎ && f > 0.0f;
                    boolean z2;
                    if (this.ॱˎ || this.ʼॱ) {
                        z2 = false;
                    } else {
                        z2 = true;
                    }
                    boolean z3;
                    if (this.ʼॱ && ˋ()) {
                        z3 = true;
                    } else {
                        z3 = false;
                    }
                    boolean z4;
                    if (!this.ʼॱ || f >= 0.0f) {
                        z4 = false;
                    } else {
                        z4 = true;
                    }
                    if (!this.ˏॱ && (z || r3 || r4 || r5)) {
                        this.ˏॱ = true;
                        motionEvent.setAction(3);
                        MotionEvent obtain = MotionEvent.obtain(motionEvent);
                        dispatchTouchEvent(motionEvent);
                        obtain.setAction(0);
                        this.ˊ = y;
                        return dispatchTouchEvent(obtain);
                    }
                }
                break;
        }
        this.ˊ = y;
        return super.dispatchTouchEvent(motionEvent);
    }

    public boolean onTouchEvent(MotionEvent motionEvent) {
        ˏ();
        this.ʽ.addMovement(motionEvent);
        int action = motionEvent.getAction();
        float y = motionEvent.getY();
        switch (action) {
            case 0:
                if (!this.ᐝ.isFinished()) {
                    this.ᐝ.abortAnimation();
                }
                this.ˋॱ = y;
                return true;
            case 1:
                if (this.ˈ != null) {
                    this.ˈ.sendMessageDelayed(this.ˈ.obtainMessage(1001), 5);
                }
                this.ͺ = false;
                this.ʽ.computeCurrentVelocity(1000, (float) this.ʻ);
                int yVelocity = (int) this.ʽ.getYVelocity();
                if (Math.abs(yVelocity) > this.ॱॱ) {
                    ˊ(yVelocity);
                }
                ˊ();
                break;
            case 2:
                float f = y - this.ˋॱ;
                if (!this.ͺ && Math.abs(f) > ((float) this.ʼ)) {
                    this.ͺ = true;
                }
                if (this.ͺ) {
                    scrollBy(0, (int) (-f));
                    if (ˎ(f)) {
                        motionEvent.setAction(0);
                        dispatchTouchEvent(motionEvent);
                        this.ˏॱ = false;
                        this.ॱᐝ = true;
                        postDelayed(new Runnable(this) {
                            final /* synthetic */ SectionNestScrollLayout ˊ;

                            {
                                this.ˊ = r1;
                            }

                            public void run() {
                                this.ˊ.ॱᐝ = false;
                            }
                        }, 50);
                    }
                }
                this.ˋॱ = y;
                break;
            case 3:
                this.ͺ = false;
                ˊ();
                if (!this.ᐝ.isFinished()) {
                    this.ᐝ.abortAnimation();
                    break;
                }
                break;
        }
        return super.onTouchEvent(motionEvent);
    }

    private void ˊ(int i) {
        int ॱ = (int) ॱ(i);
        if (i > 0) {
            if (this.ᐝ.getCurrY() - ॱ <= 0) {
                ˋ(0, IPRoute.INTERVAL);
            } else {
                ˋ(-i);
            }
        }
        if (i >= 0) {
            return;
        }
        if ((this.ॱˊ - this.ᐝ.getCurrY()) - ॱ <= 0) {
            ˋ(this.ॱˊ, IPRoute.INTERVAL);
        } else {
            ˋ(-i);
        }
    }

    private boolean ˎ(float f) {
        return this.ᐝ.isFinished() && ((getScrollY() == this.ॱˊ && f < 0.0f) || (getScrollY() == 0 && f > 0.0f && !ˋ()));
    }

    public void ˋ(int i, int i2) {
        int scrollY = getScrollY();
        this.ᐝ.startScroll(0, scrollY, 0, i - scrollY, i2);
        invalidate();
    }

    public boolean onInterceptTouchEvent(MotionEvent motionEvent) {
        int action = motionEvent.getAction();
        float y = motionEvent.getY();
        if (!ˎ(action)) {
            return true;
        }
        switch (action) {
            case 0:
                this.ˋॱ = y;
                break;
            case 2:
                float f = y - this.ˋॱ;
                if (Math.abs(f) > ((float) this.ʼ)) {
                    this.ͺ = true;
                    if (this.ʼॱ && !ˋ() && f > 0.0f && this.ᐝ.isFinished()) {
                        this.ˏॱ = false;
                        return false;
                    } else if (!this.ॱˎ) {
                        return true;
                    } else {
                        if (ˋ() && this.ॱˎ && f > 0.0f) {
                            return true;
                        }
                    }
                }
                break;
        }
        return super.onInterceptTouchEvent(motionEvent);
    }

    private boolean ˎ(int i) {
        if (!this.ˏॱ && this.ॱᐝ && i == 1 && ˋ() && this.ॱˎ) {
            return false;
        }
        if (this.ᐝ.isFinished() || !this.ˏॱ) {
            return true;
        }
        return false;
    }

    protected void onScrollChanged(int i, int i2, int i3, int i4) {
        super.onScrollChanged(i, i2, i3, i4);
        if (this.ʻॱ != null) {
            this.ʻॱ.ˏ(getScrollY(), this.ॱˋ);
        }
    }

    public void setOnScrollListener(c cVar) {
        this.ʻॱ = cVar;
    }

    public void setImmerse(boolean z) {
        this.ᐝॱ = z;
    }

    private boolean ˋ() {
        if (this.ʻॱ != null) {
            return this.ʻॱ.ॱ();
        }
        return false;
    }

    protected void onMeasure(int i, int i2) {
        try {
            super.onMeasure(i, i2);
            getChildAt(0).measure(i, MeasureSpec.makeMeasureSpec(0, 0));
            if (getChildAt(1) != null) {
                getChildAt(1).measure(i, MeasureSpec.makeMeasureSpec(0, 0));
            }
            LayoutParams layoutParams = this.ˎ.getLayoutParams();
            this.ˊॱ = this.ॱ.getMeasuredHeight();
            if (this.ˋ != null) {
                this.ॱˋ = this.ˋ.getMeasuredHeight();
            }
            this.ॱˊ = this.ˊॱ;
            if (this.ᐝॱ) {
                int ॱ = ॱ();
                layoutParams.height = (getMeasuredHeight() - this.ˏ.getMeasuredHeight()) - ॱ;
                this.ॱˊ = this.ˊॱ - ॱ;
            } else {
                layoutParams.height = (getMeasuredHeight() - this.ˏ.getMeasuredHeight()) + getResources().getDimensionPixelSize(o.cab.c.ʽ);
                this.ॱˊ = this.ˊॱ + this.ˏ.getMeasuredHeight();
            }
            setMeasuredDimension(getMeasuredWidth(), (this.ॱ.getMeasuredHeight() + this.ˏ.getMeasuredHeight()) + this.ˎ.getMeasuredHeight());
        } catch (Exception e) {
            bqx.ˏ("NestScrollLayout", "onMeasure error");
        }
    }

    public static int ॱ() {
        int dimensionPixelSize = ContentManager.getInstant().getContext().getResources().getDimensionPixelSize(o.cab.c.ॱॱ);
        if (VERSION.SDK_INT >= 21) {
            return dimensionPixelSize + epv.ॱ();
        }
        return dimensionPixelSize;
    }

    public void ˋ(int i) {
        this.ᐝ.fling(0, getScrollY(), 0, i, 0, 0, 0, this.ॱˊ);
        invalidate();
    }

    public void scrollTo(int i, int i2) {
        boolean z = true;
        this.ॱˎ = false;
        if (i2 < 0) {
            this.ʼॱ = true;
            i2 = 0;
        }
        if (i2 > this.ॱˊ) {
            i2 = this.ॱˊ;
            this.ॱˎ = true;
            this.ʼॱ = false;
        }
        if (i2 != getScrollY()) {
            super.scrollTo(i, i2);
        }
        this.ʼॱ = getScrollY() == 0;
        if (getScrollY() != this.ॱˊ) {
            z = false;
        }
        this.ॱˎ = z;
        if ((this.ॱˎ || this.ʼॱ) && this.ᐝ != null && !this.ᐝ.isFinished()) {
            this.ᐝ.abortAnimation();
        }
    }

    private double ˏ(int i) {
        return Math.log((double) ((0.35f * ((float) Math.abs(i))) / (this.ʽॱ * this.ʿ)));
    }

    private double ॱ(int i) {
        return Math.exp(ˏ(i) * (((double) ʾ) / ((double) (ʾ - 1.0f)))) * ((double) (this.ʽॱ * this.ʿ));
    }

    public void computeScroll() {
        if (this.ᐝ.computeScrollOffset()) {
            scrollTo(0, this.ᐝ.getCurrY());
            invalidate();
        }
    }

    public void setStateListener(caz o_caz) {
        this.ˈ = new b(o_caz, this);
    }

    public boolean ˎ() {
        return this.ʼॱ;
    }
}
